//
// Created by 33145 on 2023/6/8.
//

#ifndef ROCKET_RPC_DISPATCHER_H
#define ROCKET_RPC_DISPATCHER_H

#include <map>
#include <memory>
#include <google/protobuf/service.h>

#include "../coder/tinypb_protocol.h"

namespace rocket {

  class TCPConnection;

  class RPCDispatcher {
  public:
    typedef std::shared_ptr<RPCDispatcher> ptr;
    typedef std::shared_ptr<google::protobuf::Service> service_ptr;

    static ptr GetRPCDispatcher();

    RPCDispatcher() = default;

    void dispatch(const AbstractProtocol::ptr& request, const AbstractProtocol::ptr& response, TCPConnection* connection);
    void registerService(const service_ptr& service);
    void setTinyPBError(const TinyPBProtocol::ptr & msg, int32_t err_code, const std::string& err_info);

  private:
    bool parseServiceFullName(std::string& full_name, std::string& service_name, std::string& method_name);

  private:
    // 注册的Service对象
    std::map<std::string, std::shared_ptr<google::protobuf::Service>> _service_map;
  };
}

#endif //ROCKET_RPC_DISPATCHER_H
